package apis

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	config2 "github.com/go-admin-team/go-admin-core/config"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"strconv"
	"tuowei.com/app/api/service"
	"tuowei.com/app/api/service/dto"
	"tuowei.com/pkg/xlog"
)

type Pay struct {
	api.Api
}

func (e Pay) Pay(c *gin.Context) {
	req := &dto.PayReq{}
	s := service.AliPay{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		xlog.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	xlog.Infof("PayReq:%+v", req)
	tableName := ""
	//商品详情进来付款
	switch req.OrderType {
	case "OnlineOrder", "LiveOrder":
		tableName = "sys_online_order"
	case "MaterialOrder":
		tableName = "sys_material_order"
	default:
		//订单列表进来付款,不知道OrderType
		TotalAmount := 0.00
		err = e.Orm.Table("sys_online_order").Select("price").Where("order_number=?", req.OrderSn).Scan(&TotalAmount).Error
		if err != nil {
			xlog.Errorf("db error: %s", err)
			e.Error(500, err, err.Error())
			return
		}
		if TotalAmount != 0 {
			req.OrderType = "OnlineOrder"
			tableName = "sys_online_order"
			break
		}
		err = e.Orm.Table("sys_material_order").Select("price").Where("order_number=?", req.OrderSn).Scan(&TotalAmount).Error
		if err != nil {
			xlog.Errorf("db error: %s", err)
			e.Error(500, err, err.Error())
			return
		}
		if TotalAmount != 0 {
			req.OrderType = "MaterialOrder"
			tableName = "sys_material_order"
		} else {
			e.Error(500, err, "订单金额为0,请检查订单")
			return
		}
	}
	TotalAmount := 0.00
	err = e.Orm.Table(tableName).Select("price").Where("order_number=?", req.OrderSn).Scan(&TotalAmount).Error
	if err != nil {
		xlog.Errorf("db error: %s", err)
		e.Error(500, err, err.Error())
		return
	}
	if TotalAmount == 0 {
		xlog.Errorf("db outTradeNo: %s;price为0", req.OrderSn)
		e.Error(500, err, "订单金额为0,请检查订单")
		return
	}
	OrderType := ""
	switch req.OrderType {
	case "OnlineOrder":
		OrderType = "onlineorder"
	case "LiveOrder":
		OrderType = "liveorder"
	case "MaterialOrder":
		OrderType = "materialorder"
	default:
		xlog.Errorf("aliPayReq.OrderType err,aliPayReq.OrderType:%s\n", req.OrderType)
		return
	}

	switch req.PayMethod {
	case 2: //支付宝
		req1 := &dto.AlipayReq{}
		s1 := service.AliPay{}
		req1.OrderType = OrderType
		req1.Body = "body"
		req1.Subject = "subject"
		req1.OrderSn = req.OrderSn
		req1.TotalAmount = strconv.FormatFloat(TotalAmount/100, 'g', 5, 32)
		//支付宝的就是元
		payEnv := config2.Get("PAY_ENV").String("")
		if payEnv == "test" {
			req1.TotalAmount = "0.01"
		}
		//req1.ProductCode = "QUICK_MSECURITY_PAY"
		xlog.Infof("支付宝:%+v", req1)
		str, err := s1.AppPay(e.Context, req1)
		if err != nil {
			xlog.Error(err)
			e.Error(500, err, err.Error())
			return
		}
		e.OK(gin.H{"pay_params": str}, "支付参数获取成功")
		return
	case 1: //微信支付
		req2 := dto.WxPayReq{}
		s2 := service.WxPay{}
		req2.OrderType = OrderType
		req2.Body = "body"
		req2.Description = "description"
		//todo 32
		req2.OrderSn = req.OrderSn
		req2.TotalAmount = int64(TotalAmount)
		//微信支付是以分为单位的，金额进来要*100的，
		payEnv := config2.Get("PAY_ENV").String("")
		if payEnv == "test" {
			req2.TotalAmount = 1
		}
		req2.ProductCode = "QUICK_MSECURITY_PAY"
		xlog.Infof("微信支付req2:%+v", req2)
		params, err := s2.AppPay(e.Context, &req2)
		if err != nil {
			xlog.Error(err)
			e.Error(500, errors.New("支付参数错误"), err.Error())
			return
		}
		xlog.Infof("微信支付resp2:%+v", params)
		xlog.Pretty(params)
		e.OK(gin.H{"pay_params": params}, "支付参数获取成功")
		return
	}

}
